<template>
  <div class="departDialog-container">
    <div class="app-container">
      <el-dialog
        title="组织操作"
        :visible.sync="this.dialogFormVisible"
        :before-close="handleClose"
      >
        <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="form.name" style="width: 80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门编码" prop="code">
            <el-input v-model="form.code" style="width: 80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门负责人" prop="manager">
            <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
              <el-option v-for="item in employeesList" :key="item.id" :value="item.username" />
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍" prop="introduce">
            <el-input
              v-model="form.introduce"
              style="width: 80%"
              placeholder="1-300个字符"
              type="textarea"
              :rows="3"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: 'departDialog',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    employeesList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          // { validator: validName, trigger: 'blur' }, 自定义校验规则 validName 查看是否部门重名  需要父组件传递全部数据进行判断
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          // { validator: validCode, trigger: 'blur' },  自定义校验规则 validCode  查看部门编码重复
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    handleClose() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
      }
      this.$emit('update:dialogFormVisible', false)
    },
    handleConfirm() {
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.$emit('addDepart', this.form)
          this.form = {
            name: '',
            code: '',
            manager: '',
            introduce: '',
          }
          this.handleClose()
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
